// pages/my-info/my-info.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    picker: [
      {label: '身份证', value: 1}
    ],
    sexList: [
      {label: '未知', value: 0},
      {label: '男', value: 1},
      {label: '女', value: 2}
    ],
    authenticationTypeList: [
      {label: '驾驶证', value: 1},
      {label: '网约车从业资格证', value: 2},
    ],
    userForm: {
      openid: '',
      documentType: 1,
      documentNo: '',
      sex: 0,
      birthdayFormat: '',
      realName: '',
      phone: '',
      isAuthentication: 0,
      authenticationType: 1,
      authenticationNo: '',
      authenticationEndTimeFormat: '',
    },

    formSex: 0,
    formDocumentType : 1,
    formBirthdayFormat: '',
    formAuthenticationType: 1,
    formAuthenticationEndTimeFormat: '',

    documentTypeIndex: 0,
    authenticationTypeIndex: 0,
    sexIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo =  app.globalData.userInfo
    this.getUserInfo(userInfo.openid)
    // var userInfo = 
    // console.log(userInfo)
    // var form = this.data.userForm
    // for(var k in userInfo){
    //   form[k] = userInfo[k]
    // }
    // this.setData({userInfo: userInfo,userForm: form})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  getUserInfo(openid){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'wechat/getUserByOpenid',
      method: 'POST',
      data: {openid: openid},
      success (res) {
          console.log(1111,res.data)
          var info =  res.data.data
          var form = that.data.userForm
          for(var k in form){
            form[k] = info[k]
          }
          form.birthdayFormat = info.birthday
          form.authenticationEndTimeFormat = info.authenticationEndTime
          that.data.sexList.forEach((item,index) => {
            if(item.value == form.sex){ that.setData({sexIndex: index}) }
          })  
          that.data.picker.forEach((item,index) => {
            if(item.value == form.documentType){ that.setData({documentTypeIndex: index}) }
          })  
          that.data.authenticationTypeList.forEach((item,index) => {
            if(item.value == form.authenticationType){ that.setData({authenticationTypeIndex: index}) }
          }) 
          info['avatarUrl'] = info.portrait
          that.setData({
            userInfo: info,
            userForm: form,
            formSex: info.sex,
            formBirthdayFormat: info.birthday,
            formAuthenticationType: info.authenticationType,
            formAuthenticationEndTimeFormat: info.authenticationEndTime,
          })
      }
    })
  },
  bindDocumentTypeChange(e){
      var index = e.detail.value
      this.setData({formDocumentType : this.data.picker[index].value,documentTypeIndex: index})
  },
  bindSexChange(e){
    var index = e.detail.value
    this.setData({formSex : this.data.sexList[index].value,sexIndex: index})
  },
  bindDateChange(e){
    var date = e.detail.value
    this.setData({formBirthdayFormat : date})
  },
  bindAuthenticationChange(e){
    var index = e.detail.value
    this.setData({formAuthenticationType : this.data.authenticationTypeList[index].value,authenticationTypeIndex: index})
  },
  bindAuthenticationDateChange(e){
    var date = e.detail.value
    this.setData({formAuthenticationEndTimeFormat : date})
  },

  formSubmit(e){
    console.log(e)
    var detail = e.detail.value
    if(detail.realName == null || detail.realName == ''){
      wx.showToast({title: '请输入姓名',icon: 'error',duration: 2000}); return false;
    }else if(detail.documentNo == null || detail.documentNo == ''){
      wx.showToast({title: '请输入身份证号码',icon: 'error',duration: 2000}); return false;
    }else if(detail.phone == null || detail.phone == ''){
      wx.showToast({title: '请输入手机号码',icon: 'error',duration: 2000}); return false;
    }else if(detail.authenticationNo == null || detail.authenticationNo == ''){
      wx.showToast({title: '请输入认证号码',icon: 'error',duration: 2000}); return false;
    }

    var form = this.data.userForm
    form.openid = this.data.userInfo.openid
    for (var k in detail) {
      form[k] = detail[k]
    }
    form.documentType = this.data.formDocumentType
    form.sex = this.data.formSex
    form.birthdayFormat = this.data.formBirthdayFormat
    form.authenticationType = this.data.formAuthenticationType
    form.authenticationEndTime = this.data.formAuthenticationEndTimeFormat
    this.setData({userForm : form})
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'wechat/saveUserInfo',
      method: 'POST',
      data: form,
      success (res) {
          console.log(res.data)
          var user = res.data
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000,
            complete: function(){
              wx.switchTab({
                url: '/pages/my/index'
              })
            }
          })
      }
    })
  },

})